package tk.djcrazy.instantmessage.serviceImpl;

import org.springframework.stereotype.Service;

import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IUserService;

@Service
public class UserServiceImpl extends ServiceImpl<UserEntity> implements
		IUserService {
// 	private IFriendGroupService  friendGroupService;
 	@Override
	public void save(UserEntity userEntity) {
		getDao().save(userEntity);
	}

	@Override
	public boolean authenticateUser(String userName, String password) {
		boolean flag = false;
		UserEntity entity = findByUserName(userName);
		if (entity != null && entity.getPassword().equals(password)) {
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean isUserNameCanBeRegister(String userName) {
		boolean flag = false;
		UserEntity entity = findByUserName(userName);
		if (entity == null) {
			flag = true;
		}
		return flag;
	}

	@Override
	public UserEntity findByUserName(String userName) {
		UserEntity entity = (UserEntity) this.getDao().queryUnique(
				"select u from UserEntity u where u.userName=?", userName);
		return entity;
	}

	@Override
	public UserEntity findByEmail(String email) {
		UserEntity entity = (UserEntity) this.getDao().queryUnique(
				"select u from UserEntity u where u.email=?", email);
		return entity;
	}

	@Override
	public void delete(UserEntity baseBean) {
		getDao().delete(baseBean);
	}

 	@Override
	public boolean isEmailCanBeRegister(String email) {
		UserEntity entity = getDao().queryUnique(
				"from UserEntity u where u.email=?", email);
		if (entity==null) {
			return true;
		}
		return false;
	}

	@Override
	public void registerUser(String userName, String passowrd, String email) {
		UserEntity entity = new UserEntity();
		entity.setUserName(userName);
		entity.setPassword(passowrd);
		entity.setEmail(email);
		getDao().create(entity);
		//friendGroupService.addFriendGroup(entity.getId(), "未分组好友");
	}

	@Override
	public UserEntity findById(int id) {
		return getDao().findById(UserEntity.class, id);
	}

 }
